<?php

class bulletin_helper
{
    private $enable_user_ids = array();
    private $enable_store_ids = array();

    //合伙人APP简报
    public static function get_partner_app_info_list($time = ""){
        $enable_user_ids = _model('user')->getFields('id', array('is_enable' => 1, 'status' => 1, '`add_time` <= '=>date('Y-m-d').' 23:59:59'));
        // 获取所有有效的经销商
        $partner_ids = user_helper::get_role_user_ids(user_config::AGENCY, '', array('is_enable' => 1, 'status' => 1));
        if (!$partner_ids) {
            return array();
        }
        $partner_list = array();
        foreach ($partner_ids as $k => $v) {
            $cur_dealer_info = user_helper::get_user_info($v);
            // 下单数量 (是要是收款的订单就算)
            $partner_order_num = order_helper::get_order_num_by_progress(array(3, 5, 10) , 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));
            // 是否下单
            if ($partner_order_num) {
                $partner_list[$k]['is_ever_order_goods'] = '是';
            } else {
                $partner_list[$k]['is_ever_order_goods'] = '否';
            }
            //掌柜数量
            $store_list = store_helper::get_store_list_by_dealer_id($cur_dealer_info['id'], array('is_enable' => 1, 'is_delete'=>1, 'status' => array(1, 2)));
            //是否创建掌柜
            if ($store_list) {
                $partner_list[$k]['is_sign_store'] = '是';
                // 员工数量
                $partner_list[$k]['marketing_num'] = _model('user_marketing')->getTotal(array('agency_id'=>$v, 'marketing_id'=>$enable_user_ids));
            }else{
                $partner_list[$k]['is_sign_store'] = '否';
                $partner_list[$k]['marketing_num'] = 0;
            }
        }
        $partner_app_list = array();
        //合伙人数量
        $partner_app_list['partner_num'] = count($partner_list);
        //已下单
        $place_an_order = array();
        //未下单
        $un_place_an_order = array();
        //已签门店人数
        $is_sign_store = array();
        //员工总数
        $marketing_num = array();
        foreach($partner_list as $k => $v){
            if($v['is_ever_order_goods'] == "是")  $place_an_order[] = $v;

            if($v['is_ever_order_goods'] == "否")  $un_place_an_order[] = $v;

            if($v['is_sign_store'] == "是")  $is_sign_store[] = $v;

            if($v['marketing_num'] != 0)  $marketing_num[] = $v['marketing_num'];
        }

        $marketing_cnt = _model('user')->get_total_marketing_cnt();
        // 合伙人总员工：多少人 (市场人员的总数量)
        // $store_list = store_helper::get_store_list_by_dealer_id($enable_user_ids, array('is_enable'=>1, 'is_delete'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59' ));
        // $marketing_ids = array();
        // if ($store_list) {
        //     foreach ($store_list as $k=>$v) {
        //         if (!in_array($v['marketing_id'], $marketing_ids) && $v['marketing_id'] != $v['agency_id'] && $v['marketing_id'] != 0) {
        //             array_push($marketing_ids, $v['marketing_id']);
        //         }
        //     }
        // }
        //今日下单人数
        $partner_app_list['today_order_partner_num'] = count(order_helper::get_order_list_by_progress(array(1, 3, 5, 10, 15) , 1, array( 'is_enable' => 1, 'status' => 1, 'add_time >=' => date('Y-m-d', $time).' 00:00:00', 'add_time <=' => date('Y-m-d', $time).' 23:59:59')));
        $partner_app_list['place_an_order'] = count($place_an_order);
        $partner_app_list['un_place_an_order'] = count($un_place_an_order);
        $partner_app_list['is_sign_store'] = count($is_sign_store);
        // $partner_app_list['marketing_num'] = count(array_unique($marketing_ids));
        $partner_app_list['marketing_num'] = $marketing_cnt;

        return $partner_app_list;
    }

    //商家APP简报
    public static function get_manager_app_data_list($time){
        $store_list = array();

        $filter['is_enable'] = 1;
        $filter['is_delete'] = 1;
        $filter['`agency_id` != '] = 0;
        $filter['`add_time` < '] = date('Y-m-d').' 00:00:00';

        $store_list = _model('store')->getList($filter, ' ORDER BY `id` DESC');
        $manager_app_data = array();
        //已进货门店数
        $stock_manage_num = array();
        //未进货门店数
        $unstock_manage_num = array();
        //未绑定设备数
        $unstock_device_num = array();
        foreach($store_list as $k => $v){
            $store_list[$k]['is_ever_stock'] = store_helper::get_store_stock_money($v['id'], array(5, 10));
            $store_list[$k]['stock_num']     = 0;
            if ($store_list[$k]['is_ever_stock']) {
                $stock_manage_num[] = '是';
            } else {
                $unstock_manage_num[] = '否';
            }

            // 是否绑定设备
            $is_bind_device = device_helper::is_bind_device($v['id']);
            if (!$is_bind_device) {
                $unstock_device_num[] = '否';
            }
        }
        $manager_app_data['store_num'] = count($store_list);
        $manager_app_data['stock_manage_num'] = count($stock_manage_num);
        $manager_app_data['unstock_manage_num'] = count($unstock_manage_num);
        $manager_app_data['unstock_device_num'] = count($unstock_device_num);

        return $manager_app_data;
    }

    //合伙人订单简报
    public static function get_partner_order_list($time = ""){
        $partner_order_list = array();
        $partner_order_list['partner_order_num'] = order_helper::get_order_num_by_progress(array(1, 3, 5, 10, 15), 1);
        //新增订单
        $partner_order_list['partner_new_order_num'] = order_helper::get_order_num_by_progress(array(1, 3, 5, 10, 15) , 1, array( 'is_enable' => 1, 'status' => 1, 'add_time >=' => date('Y-m-d', $time).' 00:00:00', 'add_time <=' => date('Y-m-d', $time).' 23:59:59'));
        // 待收款
        $partner_order_list['partner_prepare_pay_order_num']      = order_helper::get_order_num_by_progress(1 , 1, array( 'is_enable' => 1, 'status' => 1));
        // 待配送
        $partner_order_list['partner_prepare_delivery_order_num'] = order_helper::get_order_num_by_progress(3 , 1, array('is_enable' => 1, 'status' => 1));
        // 配送中
        $partner_order_list['partner_in_delivery_order_num']      = order_helper::get_order_num_by_progress(5 , 1, array('is_enable' => 1, 'status' => 1));
        // 配送完成
        $partner_order_list['partner_finish_delivery_order_num']  = order_helper::get_order_num_by_progress(10 , 1, array('is_enable' => 1, 'status' => 1));
        // 撤销订单
        $partner_order_list['partner_dealer_cancel_order_num']    = order_helper::get_order_num_by_progress(15 , 1, array('is_enable' => 1, 'status' => 1));

        return $partner_order_list;
    }

    //商家订单简报
    public static function get_manager_order_data_list($time = ""){
        $manager_order_list = array();
        $manager_order_list['manager_order_num'] = order_helper::get_order_num_by_progress(array(1, 3, 5, 10, 15), 5);
        //新增订单
        $manager_order_list['manager_new_order_num'] = order_helper::get_order_num_by_progress(array(1, 3, 5, 10, 15) , 5, array( 'is_enable' => 1, 'status' => 1, 'add_time >=' => date('Y-m-d', $time).' 00:00:00', 'add_time <=' => date('Y-m-d', $time).' 23:59:59'));
        // 撤销订单
        $manager_order_list['manager_dealer_cancel_order_num']    = order_helper::get_order_num_by_progress(15 , 5, array('is_enable' => 1, 'status' => 1));

        return $manager_order_list;
    }

    //合伙人设备简报
    public static function get_device_data_list($time){
        $relation['type'] = array('MX', 'ZX', 'FX','AD','TA','OS','CS');
        $relation['status'] = 2;
        $device_by = ' ORDER BY `id` DESC ';
        $relation_info = _model('store_device_relation')->getList($relation, $device_by);
        foreach ($relation_info as $k => $v) {
            $relation_info[$k]['device_no'] = device_helper::get_device_code_by_id($v['dev_id']);
        }
        foreach($relation_info as $k => $v){
            if($relation_info[$k]['type'] == 'MX'){
                $mx_machine_num[] = $v;
            }
            if($relation_info[$k]['type'] == 'ZX'){
                $zx_machine_num[] = $v;
            }
            if($relation_info[$k]['type'] == 'FX'){
                $fx_machine_num[] = $v;
            }
            if($relation_info[$k]['type'] == 'AD'){
                $ad_machine_num[] = $v;
            }
            if($relation_info[$k]['type'] == 'TA'){
                $ta_machine_num[] = $v;
            }
            if($relation_info[$k]['type'] == 'OS'){
                $os_machine_num[] = $v;
            }
            if($relation_info[$k]['type'] == 'CS'){
                $cs_machine_num[] = $v;
            }
        }

        $device_data = array();
        $device_data['mx_machine_num'] = count($mx_machine_num);
        $device_data['zx_machine_num'] = count($zx_machine_num);
        $device_data['fx_machine_num'] = count($fx_machine_num);
        $device_data['ad_machine_num'] = count($ad_machine_num);
        $device_data['ta_machine_num'] = count($ta_machine_num);
        $device_data['os_machine_num'] = count($os_machine_num);
        $device_data['cs_machine_num'] = count($cs_machine_num);
        $device_data['all_machine_num'] = count($relation_info);

        return $device_data;
    }
    /**
     * [App订单简报导出]
     * 数据格式严格按照bulletin_config设定
     * @param  array  $app_bulletin [description]
     * @return [array]               [description]
     */
    public static function decorator_app_bulletin_export($app_bulletin = array())
    {
        $lines = array();
        foreach ($app_bulletin as $k => $v) {
            if (! isset($v['partner_dealer_data']['today_order_partner_num'])) {
                $v['partner_dealer_data']['today_order_partner_num'] = 0;
            }
            array_push($lines, array(
                'date' => $v['date'],
                'partner_cnt' => $v['partner_dealer_data']['partner_num'],
                'partner_order_day_cnt' => $v['partner_dealer_data']['today_order_partner_num'],
                'partner_order_cnt' => $v['partner_dealer_data']['place_an_order'],
                'partner_no_order_cnt' => $v['partner_dealer_data']['un_place_an_order'],
                'store_sign_cnt' => $v['partner_dealer_data']['is_sign_store'],
                'marketing_cnt' => $v['partner_dealer_data']['marketing_num'],
                'store_cnt' => $v['manager_dealer_data']['store_num'],
                'store_stock_cnt' => $v['manager_dealer_data']['stock_manage_num'],
                'store_unstock_cnt' => $v['manager_dealer_data']['unstock_manage_num'],
                'unbound_device_cnt' => $v['manager_dealer_data']['unstock_device_num'],
            ));
        }
        return $lines;
    }
}
